#include "headers/Enemies.h"
#include "headers/FileLoader.h"
#include "headers/Windows.h"
#include "headers/PPUSystem.h"
#include "headers/LevelMenager.h"
#include "headers/Etc.h"

#include <wx/utils.h>

extern FileLoader cFileLoader;
extern WindowsMenager cWindowsMenager;
extern PPUSystem cPPUSystem;
extern Enemies cEnemies;
extern LevelMenager cLevelMenager;

extern int LeftDown;

#define cLM     cLevelMenager

Enemy* EnemyClasses [] = {
    
    new Spider(),
    new Drop(),
    new Ant(),
    new Bird(),
    new Apple(),
    new Cherry(),
    new Crystal()
    
};

inline Enemy* GetEnemyClass(int type) {
    
    switch (type) {
        
        case 0x00:
            return EnemyClasses[0];
        case 0x05:
            return EnemyClasses[1];
        case 0x06:
            return EnemyClasses[2];
        case 0x08:
            return EnemyClasses[3];
        case 0x02:
            return EnemyClasses[4];
        case 0x03:
            return EnemyClasses[5];
        case 0x0C:
            return EnemyClasses[6];
            
    }
    
    return NULL;
    
}

int Enemies::MouseClick(int x, int y) {
    
    cFileLoader.SetBank(1);
    
    //przejrzyj dane o wszystkich wrogach w planszy
    for(int i=0;;i+=3) {
        //sprawdz, czy wrog nalezy do aktualnej planszy
        if (cFileLoader.Read(cLM.GetEnemyAdr()+i) != cLM.GetID()) 
            break;
        
        //wczytaj ID
        int id = cFileLoader.Read(cLM.GetEnemyAdr()+i+2);
        
        //wybierz odpowiednia klase dla wroga
        Enemy* E = GetEnemyClass(cFileLoader.Read(cLM.GetEnemyAdr()+i+1));
        
        //sprawdz, czy kliknieto mysza na wroga      
        if ( E!=NULL && E->MouseClick( id, x, y, current==i ? 1 : 0 ) ) {
            current = i;
            return 1;
        }
        
        //jest wiecej wrogow niz powinno byc
        if (i>100) {
            SomethingWrong();
            return 1;
        }                                 
    }
    
    //jesli nie kliknieto mysza na wroga:
    current = -1;
    return 0;    
}

int Enemies::Draw() {

    cFileLoader.SetBank(1);
    
    //przesun aktualnie wybranego wroga
    if ( current>=0 ) {
        Enemy* E = GetEnemyClass( cFileLoader.Read(cLM.GetEnemyAdr()+current+1) );
        E->Move( cFileLoader.Read(cLM.GetEnemyAdr()+current+2) );
    }     
     
    //rysuj sprajty wszystkich wrogow na planszy
    for(int i=0;;i+=3) {
        //sprawdz, czy wrog nalezy do tej planszy
        if (cFileLoader.Read(cLM.GetEnemyAdr()+i) != cLM.GetID()) 
            break;
        
        //odpowiednia klasa
        Enemy* E = GetEnemyClass( cFileLoader.Read(cLM.GetEnemyAdr()+i+1) );
        
        if (E!=NULL) 
            E->Draw(WindowName.c_str(), cFileLoader.Read(cLM.GetEnemyAdr()+i+2));
        
        //wiecej wrogow niz powinno byc    
        if (i>100) {
            SomethingWrong();
            return 1;
        }
    }
    
    //rysuj ramke aktualnie wybranego wroga
    if ( current>=0 ) {
        Enemy* E = GetEnemyClass( cFileLoader.Read(cLM.GetEnemyAdr()+current+1) );
        E->DrawFrames( cFileLoader.Read(cLM.GetEnemyAdr()+current+2) );
    }

}
 
int Enemies::Reset() {
    
    current = -1;
    
}
    
int Enemies::SetWindowName(const std::string& Name) {
    
    WindowName = Name;
    
}


/********************************************
****************   SPIDER  ******************
********************************************/


Spider::Spider() { }

int Spider::Draw(const char* Name, int id) {
    
    int x;
    int y1;
    int y2;
    
    x = cFileLoader.Read(0x8ACE + id*4 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ACE + id*4);
    
    y1 = cFileLoader.Read(0x8ACE + id*4 + 2);
    y2 = cFileLoader.Read(0x8ACE + id*4 + 3);
    
    //lina pajaka
    cWindowsMenager.DrawPixel(Name, GetRGBFromNesColor(0x16), x+7, y1, 2, y2-y1);
    
    //rysuj sprajt z pajakiem
    cPPUSystem.DrawPattern(SPRITES, 0, 2, Name, x, y2);
    cPPUSystem.DrawPattern(SPRITES, 1, 2, Name, x+8, y2);
    cPPUSystem.DrawPattern(SPRITES, 2, 2, Name, x, y2+8);
    cPPUSystem.DrawPattern(SPRITES, 3, 2, Name, x+8, y2+8);
    
}

int Spider::DrawFrame(const char* Name, int id) {
}

int Spider::MouseClick(int id, int mx, int my, int again) {
    
    int x;
    int y1;
    int y2;
    
    x = cFileLoader.Read(0x8ACE + id*4 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ACE + id*4);
    
    y1 = cFileLoader.Read(0x8ACE + id*4 + 2);
    y2 = cFileLoader.Read(0x8ACE + id*4 + 3);
    
    //sprawdz, czy kliknieto na pajaka
    if (mx>=x && mx<=(x+16) && my>=y1 && my<=(y2+16)) {
        mouse_x = mx-x;
        mouse_y = my-y1;
        marked = 1;
        style = normal;
        
        if (my <=y1+5)
            style = up;
        
        if (my >=y2-5+16) {
            style = down;
            mouse_y = y2 - my;
        }
        
        return 1;
    }
    
    return 0;   
}

int Spider::Move(int id) {
    
    int x;
    int y1, y2;
    
    int current_mouse_x, current_mouse_y;
       
    x = cFileLoader.Read(0x8ACE + id*4 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ACE + id*4);
    
    y1 = cFileLoader.Read(0x8ACE + id*4 + 2);
    y2 = cFileLoader.Read(0x8ACE + id*4 + 3);
    
       
    if (!LeftDown)
        marked = 0;
        
    if (marked) {

        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        switch (style) {
            case normal: {
                //set x          
                x = current_mouse_x - mouse_x; //przesun x
                    
                if (x<0) x=0; //sprawdz zasieg
                if (x>cLevelMenager.GetPixelsX()-8) x=cLevelMenager.GetPixelsX()-8;
                
                //set y
                int temp = y2-y1;
                y1 = current_mouse_y - mouse_y;
                
                if (y1<0) y1=0;
                y2 = y1 + temp;
                if (y2>192-16) y2=192-16;
                y1 = y2-temp;
                
                break;
            }

            case up:
                y1 = current_mouse_y - mouse_y;
                
                if (y1<0) y1=0;
                if (y1>y2-10) y1=y2-10;
                
                break;
            case down:
                y2 = current_mouse_y + mouse_y;
                               
                if (y2>cLevelMenager.GetPixelsY()-16) y2=cLevelMenager.GetPixelsY()-16;
                if (y2<y1+10) y2=y1+10;
                
                break;                
            
        }
        
        cFileLoader.Write(0x8ACE + id*4, x&0xff);
        cFileLoader.Write(0x8ACE + id*4 + 1, (x>>8)&0xff);
        cFileLoader.Write(0x8ACE + id*4 + 2, y1);
        cFileLoader.Write(0x8ACE + id*4 + 3, y2);
        
    }
    
    return 0;
}  

int Spider::DrawFrames(int id) {
    
    int x;
    int y1;
    int y2;
    
    x = cFileLoader.Read(0x8ACE + id*4 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ACE + id*4);
    
    y1 = cFileLoader.Read(0x8ACE + id*4 + 2);
    y2 = cFileLoader.Read(0x8ACE + id*4 + 3);
    
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(255,0,0), x, y1, 16, y2-y1+16);
    
}
    
    
/********************************************
*****************   BIRD  *******************
********************************************/


Bird::Bird() { }
    
int Bird::Draw(const char* Name, int id) {
    
    int x1, x2;
    int y1, y2;
       
    x1 = cFileLoader.Read(0x8d66 + id*7 + 1);
    x1 <<= 8;
    x1 |= cFileLoader.Read(0x8d66 + id*7);
    
    x2 = cFileLoader.Read(0x8d66 + id*7 + 3);
    x2 <<= 8;
    x2 |= cFileLoader.Read(0x8d66 + id*7 + 2);
    
    y1 = cFileLoader.Read(0x8d66 + id*7 + 4);
    y2 = cFileLoader.Read(0x8d66 + id*7 + 5);
    
    //rysuj sprajta birda
    cPPUSystem.DrawPattern(SPRITES, 6, 2, Name, x1, y1);
    cPPUSystem.DrawPattern(SPRITES, 7, 2, Name, x1+8, y1);
    
}

int Bird::DrawFrame(const char* Name, int id) {
}

int Bird::MouseClick(int id, int mx, int my, int again) {
    
    int x1, x2;
    int y1, y2;
       
    x1 = cFileLoader.Read(0x8d66 + id*7 + 1);
    x1 <<= 8;
    x1 |= cFileLoader.Read(0x8d66 + id*7);
    
    x2 = cFileLoader.Read(0x8d66 + id*7 + 3);
    x2 <<= 8;
    x2 |= cFileLoader.Read(0x8d66 + id*7 + 2);
    
    y1 = cFileLoader.Read(0x8d66 + id*7 + 4);
    y2 = cFileLoader.Read(0x8d66 + id*7 + 5);
    
    //sprawdz, czy kliknieto na birda kolejny raz
    if ( again && (x1<=mx) && (mx<=x2) && (y1<=my) && (my<=y2) ) {   
        mouse_x = mx-x1;
        mouse_y = my-y1;
        marked = 1;
        style = normal;
        
        if (mx <=x1+5)
            style = left;
        
        if (mx >=x2-5) {
            style = right;
            mouse_x = x2 - mx;
        }
        if (my <=y1+5)
            style = up;
        
        if (my >=y2-5) {
            style = down;
            mouse_y = y2 - my;
        }
            
        return 1;
    }
    //sprawdz, czy kliknieto pierwszy raz
    else if ((x1<=mx) && (mx<=x1+16) && (y1<=my) && (my<=y1+8)) {
        mouse_x = mx-x1;
        mouse_y = my-y1;
        marked = 1;
        style = normal;
        
        return 1;
    }         
        
    return 0;
}

int Bird::Move(int id) {
    
    int x1, x2;
    int y1, y2;
    
    int current_mouse_x, current_mouse_y;
       
    x1 = cFileLoader.Read(0x8d66 + id*7 + 1);
    x1 <<= 8;
    x1 |= cFileLoader.Read(0x8d66 + id*7);
    
    x2 = cFileLoader.Read(0x8d66 + id*7 + 3);
    x2 <<= 8;
    x2 |= cFileLoader.Read(0x8d66 + id*7 + 2);
    
    y1 = cFileLoader.Read(0x8d66 + id*7 + 4);
    y2 = cFileLoader.Read(0x8d66 + id*7 + 5);
    
    if (!LeftDown)
        marked = 0;
        
    if (marked) {
    
        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        switch (style) {
            case normal: {
                //set x      
                int temp = x2-x1; //szerokosc      
                x1 = current_mouse_x - mouse_x; //przesun x
                    
                if (x1<0) x1=0; //sprawdz zasieg
                x2 = x1 + temp; //ustaw x2 (taka sama szerokosc jak przed zmiana)
                if (x2>cLevelMenager.GetPixelsX()) x2=cLevelMenager.GetPixelsX();
                x1 = x2 - temp;
                
                //set y
                temp = y2-y1;
                y1 = current_mouse_y - mouse_y;
                
                if (y1<0) y1=0;
                y2 = y1 + temp;
                if (y2>192) y2=192;
                y1 = y2-temp;
                
                break;
            }
            case left:               
                x1 = current_mouse_x - mouse_x;
                
                if (x1<0) x1=0;
                if (x1>x2-20) x1=x2-20;
               
                break;
            case right:
                x2 = current_mouse_x + mouse_x;
                               
                if (x2>cLevelMenager.GetPixelsX()) x2=cLevelMenager.GetPixelsX();
                if (x2<x1+20) x2=x1+20;
                
                break;
            case up:
                y1 = current_mouse_y - mouse_y;
                
                if (y1<0) y1=0;
                if (y1>y2-10) y1=y2-10;
                
                break;
            case down:
                y2 = current_mouse_y + mouse_y;
                               
                if (y2>cLevelMenager.GetPixelsY()) y2=cLevelMenager.GetPixelsY();
                if (y2<y1+10) y2=y1+10;
                
                break;                
            
        }
        
        cFileLoader.Write(0x8d66 + id*7, x1&0xff);
        cFileLoader.Write(0x8d66 + id*7 + 1, (x1>>8)&0xff);
        cFileLoader.Write(0x8d66 + id*7 + 2, x2&0xff);
        cFileLoader.Write(0x8d66 + id*7 + 3, (x2>>8)&0xff);
        cFileLoader.Write(0x8d66 + id*7 + 4, y1);
        cFileLoader.Write(0x8d66 + id*7 + 5, y2);
        
    }
    
    return 0;
}

int Bird::DrawFrames(int id) {
    
    int x1, x2;
    int y1, y2;
       
    x1 = cFileLoader.Read(0x8d66 + id*7 + 1); x1 <<= 8;
    x1 |= cFileLoader.Read(0x8d66 + id*7);  
    x2 = cFileLoader.Read(0x8d66 + id*7 + 3); x2 <<= 8;
    x2 |= cFileLoader.Read(0x8d66 + id*7 + 2);
    y1 = cFileLoader.Read(0x8d66 + id*7 + 4);
    y2 = cFileLoader.Read(0x8d66 + id*7 + 5);    
    
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(255,0,0), x1, y1, x2-x1, y2-y1);
    
}

    
/********************************************
*****************   ANT  ********************
********************************************/


Ant::Ant() { }
    
int Ant::Draw(const char* Name, int id) {
    
    int x1, x2;
    int y;
       
    x1 = cFileLoader.Read(0x8eaf + id*8 + 1);
    x1 <<= 8;
    x1 |= cFileLoader.Read(0x8eaf + id*8);
    
    x2 = cFileLoader.Read(0x8eaf + id*8 + 3);
    x2 <<= 8;
    x2 |= cFileLoader.Read(0x8eaf + id*8 + 2);
    
    y = cFileLoader.Read(0x8eaf + id*8 + 4);
    
    //rysuj sprajta anta
    cPPUSystem.DrawPattern(SPRITES, 8, 2, Name, x2-16, y);
    cPPUSystem.DrawPattern(SPRITES, 9, 2, Name, x2-8, y);
    
}

int Ant::DrawFrame(const char* Name, int id) {
}

int Ant::MouseClick(int id, int mx, int my, int again) {
    
    int x1, x2;
    int y;
       
    x1 = cFileLoader.Read(0x8eaf + id*8 + 1);
    x1 <<= 8;
    x1 |= cFileLoader.Read(0x8eaf + id*8);
    
    x2 = cFileLoader.Read(0x8eaf + id*8 + 3);
    x2 <<= 8;
    x2 |= cFileLoader.Read(0x8eaf + id*8 + 2);
    
    y = cFileLoader.Read(0x8eaf + id*8 + 4);
    
    if ( again && (x1<=mx) && (mx<=x2+16) && (y<=my) && (my<=y+8) ) {   
        mouse_x = mx-x1;
        mouse_y = y-my;
        marked = 1;
        style = normal;
        
        if (mx <=x1+5)
            style = left;
        
        if (mx >=x2+16-5) {
            style = right;
            mouse_x = x2 - mx;
        }
 
        return 1;
    } else
    if ( (x2-16<=mx) && (mx<=x2) && (y<=my) && (my<=y+8) ) {
        mouse_x = mx-x1;
        mouse_y = y-my;
        marked = 1;
        style = normal;
        
        return 1;
    }     
        
    return 0;
}

int Ant::Move(int id) {
    
    int x1, x2;
    int y;
    
    int current_mouse_x, current_mouse_y;
       
    x1 = cFileLoader.Read(0x8eaf + id*8 + 1);
    x1 <<= 8;
    x1 |= cFileLoader.Read(0x8eaf + id*8);
    
    x2 = cFileLoader.Read(0x8eaf + id*8 + 3);
    x2 <<= 8;
    x2 |= cFileLoader.Read(0x8eaf + id*8 + 2);
    
    y = cFileLoader.Read(0x8eaf + id*8 + 4);
    
    
    if (!LeftDown)
        marked = 0;
        
    if (marked) {
    
        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        switch (style) {
            case normal: {
                //set x      
                int temp = x2-x1;      
                x1 = current_mouse_x - mouse_x;
                    
                if (x1<0) x1=0;
                x2 = x1 + temp;
                if (x2>cLevelMenager.GetPixelsX()) x2=cLevelMenager.GetPixelsX();
                x1 = x2 - temp;
                
                //set y
                y = mouse_y + current_mouse_y;
                
                if (y<0) y=0;
                if (y>184) y=184;
                
                break;
            }
            case left:               
                x1 = current_mouse_x - mouse_x;
                
                if (x1<0) x1=0;
                if (x1>x2-20) x1=x2-20;
               
                break;
            case right:
                x2 = current_mouse_x + mouse_x;
                               
                if (x2>cLevelMenager.GetPixelsX()) x2=cLevelMenager.GetPixelsX();
                if (x2<x1+20) x2=x1+20;
                
                break;           
            
        }
        
        cFileLoader.Write(0x8eaf + id*8, x1&0xff);
        cFileLoader.Write(0x8eaf + id*8 + 1, (x1>>8)&0xff);
        cFileLoader.Write(0x8eaf + id*8 + 2, x2&0xff);
        cFileLoader.Write(0x8eaf + id*8 + 3, (x2>>8)&0xff);
        cFileLoader.Write(0x8eaf + id*8 + 4, y);
        
    }
    
    return 0;
}

int Ant::DrawFrames(int id) {
    
    int x1, x2;
    int y;
       
    x1 = cFileLoader.Read(0x8eaf + id*8 + 1); x1 <<= 8;
    x1 |= cFileLoader.Read(0x8eaf + id*8);    
    x2 = cFileLoader.Read(0x8eaf + id*8 + 3); x2 <<= 8;
    x2 |= cFileLoader.Read(0x8eaf + id*8 + 2);  
    y = cFileLoader.Read(0x8eaf + id*8 + 4);
        
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(255,0,0), x1, y, x2+16-x1, 8);

}

    
/********************************************
*****************   DROP  *******************
********************************************/


Drop::Drop() { }
    
int Drop::Draw(const char* Name, int id) {
    
    int x;
    int y1, y2;
       
    x = cFileLoader.Read(0x8ca8 + id*5 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ca8 + id*5);
    
    y1 = cFileLoader.Read(0x8ca8 + id*5 + 2);
    y2 = cFileLoader.Read(0x8ca8 + id*5 + 3);
    
    //rysuj zasieg dropa
    cWindowsMenager.DrawPixel(Name, GetRGB(255,0,255), x, y1, 8, 1);
    cWindowsMenager.DrawPixel(Name, GetRGB(255,0,255), x, y1, 1, y2-y1+8);
    cWindowsMenager.DrawPixel(Name, GetRGB(255,0,255), x+8, y1, 1, y2-y1+8);
    cWindowsMenager.DrawPixel(Name, GetRGB(255,0,255), x, y2+8, 8, 1);
    
    //rysuj sprajta dropa
    cPPUSystem.DrawPattern(SPRITES, 10, 2, Name, x, y1);
    
}

int Drop::DrawFrame(const char* Name, int id) {
}

int Drop::MouseClick(int id, int mx, int my, int again) {
    
    int x;
    int y1, y2;
       
    x = cFileLoader.Read(0x8ca8 + id*5 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ca8 + id*5);
    
    y1 = cFileLoader.Read(0x8ca8 + id*5 + 2);
    y2 = cFileLoader.Read(0x8ca8 + id*5 + 3);
    
    //sprawdz, czy kliknieto na dropa
    if ( (x<=mx) && (mx<=x+8) && (y1<=my) && (my<=y2+8) ) {   
        mouse_x = mx-x;
        mouse_y = my-y1;
        marked = 1;
        style = normal;
        
        if (my <=y1+5)
            style = up;
        
        if (my >=y2-5+8) {
            style = down;
            mouse_y = y2 - my;
        }
            
        return 1;
    }
        
    return 0;
}

int Drop::Move(int id) {
    
    int x;
    int y1, y2;
    
    int current_mouse_x, current_mouse_y;
       
    x = cFileLoader.Read(0x8ca8 + id*5 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8ca8 + id*5);
    
    y1 = cFileLoader.Read(0x8ca8 + id*5 + 2);
    y2 = cFileLoader.Read(0x8ca8 + id*5 + 3);
    
    if (!LeftDown)
        marked = 0;
        
    if (marked) {
    
        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        switch (style) {
            case normal: {
                //set x          
                x = current_mouse_x - mouse_x; //przesun x
                    
                if (x<0) x=0; //sprawdz zasieg
                if (x>cLevelMenager.GetPixelsX()-8) x=cLevelMenager.GetPixelsX()-8;
                
                //set y
                int temp = y2-y1;
                y1 = current_mouse_y - mouse_y;
                
                if (y1<0) y1=0;
                y2 = y1 + temp;
                if (y2>192) y2=192;
                y1 = y2-temp;
                
                break;
            }

            case up:
                y1 = current_mouse_y - mouse_y;
                
                if (y1<0) y1=0;
                if (y1>y2-10) y1=y2-10;
                
                break;
            case down:
                y2 = current_mouse_y + mouse_y;
                               
                if (y2>cLevelMenager.GetPixelsY()) y2=cLevelMenager.GetPixelsY();
                if (y2<y1+10) y2=y1+10;
                
                break;                
            
        }
        
        cFileLoader.Write(0x8ca8 + id*5, x&0xff);
        cFileLoader.Write(0x8ca8 + id*5 + 1, (x>>8)&0xff);
        cFileLoader.Write(0x8ca8 + id*5 + 2, y1);
        cFileLoader.Write(0x8ca8 + id*5 + 3, y2);
        
    }
    
    return 0;
}

int Drop::DrawFrames(int id) {
}
    
/********************************************
*****************   APPLE  ******************
********************************************/


Apple::Apple() { }
    
int Apple::Draw(const char* Name, int id) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x8bbb + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8bbb + id*3);
    
    y = cFileLoader.Read(0x8bbb + id*3 + 2);
       
    //rysuj sprajta
    cPPUSystem.DrawPattern(SPRITES, 11, 1, Name, x, y-8);
    cPPUSystem.DrawPattern(SPRITES, 12, 1, Name, x+8, y-8);
    cPPUSystem.DrawPattern(SPRITES, 13, 1, Name, x, y);
    cPPUSystem.DrawPattern(SPRITES, 14, 1, Name, x+8, y);
    
}

int Apple::DrawFrame(const char* Name, int id) {
}

int Apple::MouseClick(int id, int mx, int my, int again) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x8bbb + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8bbb + id*3);
    
    y = cFileLoader.Read(0x8bbb + id*3 + 2);
    
    //sprawdz, czy kliknieto na japko
    if ( (x<=mx) && (mx<=x+16) && (y-8<=my) && (my<=y+8) ) {   
        mouse_x = mx-x;
        mouse_y = my-y;
        marked = 1;
                    
        return 1;
    }
        
    return 0;
}

int Apple::Move(int id) {
       
    int x;
    int y;
    
    int current_mouse_x, current_mouse_y;
       
    x = cFileLoader.Read(0x8bbb + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8bbb + id*3);
    
    y = cFileLoader.Read(0x8bbb + id*3 + 2);
    
    
    if (!LeftDown)
        marked = 0;
        
    if (marked) {
    
        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        //set x          
        x = current_mouse_x - mouse_x; //przesun x
                    
        if (x<0) x=0; //sprawdz zasieg
        if (x>cLevelMenager.GetPixelsX()-8) x=cLevelMenager.GetPixelsX()-8;
                
        //set y
        y = current_mouse_y - mouse_y;
                
        if (y<8) y=8;
        if (y+8>192) y=192-8;
                
        
        cFileLoader.Write(0x8bbb + id*3, x&0xff);
        cFileLoader.Write(0x8bbb + id*3 + 1, (x>>8)&0xff);
        cFileLoader.Write(0x8bbb + id*3 + 2, y);
        
    }
    
    return 0;
}

int Apple::DrawFrames(int id) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x8bbb + id*3 + 1); x <<= 8;
    x |= cFileLoader.Read(0x8bbb + id*3);
    y = cFileLoader.Read(0x8bbb + id*3 + 2);
    
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(0,255,0), x, y-8, 16, 16);
}

    
/********************************************
*****************   CHERRY  *****************
********************************************/


Cherry::Cherry() { }
    
int Cherry::Draw(const char* Name, int id) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x8c33 + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8c33 + id*3);
    
    y = cFileLoader.Read(0x8c33 + id*3 + 2);
       
    //rysuj sprajta
    cPPUSystem.DrawPattern(SPRITES, 15, 1, Name, x, y-8);
    cPPUSystem.DrawPattern(SPRITES, 16, 1, Name, x+8, y-8);
    cPPUSystem.DrawPattern(SPRITES, 17, 1, Name, x, y);
    cPPUSystem.DrawPattern(SPRITES, 18, 1, Name, x+8, y);
    
}

int Cherry::DrawFrame(const char* Name, int id) {
}

int Cherry::MouseClick(int id, int mx, int my, int again) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x8c33 + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8c33 + id*3);
    
    y = cFileLoader.Read(0x8c33 + id*3 + 2);
    
    //sprawdz, czy kliknieto na wisnie
    if ( (x<=mx) && (mx<=x+16) && (y-8<=my) && (my<=y+8) ) {   
        mouse_x = mx-x;
        mouse_y = my-y;
        marked = 1;
                    
        return 1;
    }
        
    return 0;
}

int Cherry::Move(int id) {
       
    int x;
    int y;
    
    int current_mouse_x, current_mouse_y;
       
    x = cFileLoader.Read(0x8c33 + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x8c33 + id*3);
    
    y = cFileLoader.Read(0x8c33 + id*3 + 2);
    
    
    if (!LeftDown)
        marked = 0;
        
    if (marked) {
    
        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        //set x          
        x = current_mouse_x - mouse_x; //przesun x
                    
        if (x<0) x=0; //sprawdz zasieg
        if (x>cLevelMenager.GetPixelsX()-8) x=cLevelMenager.GetPixelsX()-8;
                
        //set y
        y = current_mouse_y - mouse_y;
                
        if (y<8) y=8;
        if (y+8>192) y=192-8;
                
        
        cFileLoader.Write(0x8c33 + id*3, x&0xff);
        cFileLoader.Write(0x8c33 + id*3 + 1, (x>>8)&0xff);
        cFileLoader.Write(0x8c33 + id*3 + 2, y);
        
    }
    
    return 0;
}

int Cherry::DrawFrames(int id) {
        
    int x;
    int y;
       
    x = cFileLoader.Read(0x8c33 + id*3 + 1); x <<= 8;
    x |= cFileLoader.Read(0x8c33 + id*3);  
    y = cFileLoader.Read(0x8c33 + id*3 + 2);
    
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(0,255,0), x, y-8, 16, 16);
}
    
/********************************************
****************   CRYSTAL  *****************
********************************************/


Crystal::Crystal() { }
    
int Crystal::Draw(const char* Name, int id) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x908f + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x908f + id*3);
    
    y = cFileLoader.Read(0x908f + id*3 + 2);
       
    //rysuj sprajta
    cPPUSystem.DrawPattern(SPRITES, 19, 0, Name, x, y-8);
    cPPUSystem.DrawPattern(SPRITES, 20, 0, Name, x+8, y-8);
    cPPUSystem.DrawPattern(SPRITES, 21, 0, Name, x, y);
    cPPUSystem.DrawPattern(SPRITES, 22, 0, Name, x+8, y);
    
}

int Crystal::DrawFrame(const char* Name, int id) {
}

int Crystal::MouseClick(int id, int mx, int my, int again) {
    
    int x;
    int y;
       
    x = cFileLoader.Read(0x908f + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x908f + id*3);
    
    y = cFileLoader.Read(0x908f + id*3 + 2);
    
    //sprawdz, czy kliknieto na krysztal
    if ( (x<=mx) && (mx<=x+16) && (y-8<=my) && (my<=y+8) ) {   
        mouse_x = mx-x;
        mouse_y = my-y;
        marked = 1;
                    
        return 1;
    }
        
    return 0;
}

int Crystal::Move(int id) {
       
    int x;
    int y;
    
    int current_mouse_x, current_mouse_y;
       
    x = cFileLoader.Read(0x908f + id*3 + 1);
    x <<= 8;
    x |= cFileLoader.Read(0x908f + id*3);
    
    y = cFileLoader.Read(0x908f + id*3 + 2);
    
    
    if (!LeftDown)
        marked = 0;
        
    if (marked) {
    
        cWindowsMenager.GetMouseState(cEnemies.GetWindowName().c_str(), &current_mouse_x, &current_mouse_y);
        
        //set x          
        x = current_mouse_x - mouse_x; //przesun x
                    
        if (x<0) x=0; //sprawdz zasieg
        if (x>cLevelMenager.GetPixelsX()-8) x=cLevelMenager.GetPixelsX()-8;
                
        //set y
        y = current_mouse_y - mouse_y;
                
        if (y<8) y=8;
        if (y+8>192) y=192-8;
                
        
        cFileLoader.Write(0x908f + id*3, x&0xff);
        cFileLoader.Write(0x908f + id*3 + 1, (x>>8)&0xff);
        cFileLoader.Write(0x908f + id*3 + 2, y);
        
    }
    
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(0,255,0), x, y-8, 16, 16);
    
    return 0;
}

int Crystal::DrawFrames(int id) {

    int x;
    int y;
       
    x = cFileLoader.Read(0x908f + id*3 + 1); x <<= 8;
    x |= cFileLoader.Read(0x908f + id*3);  
    y = cFileLoader.Read(0x908f + id*3 + 2);
    
    cWindowsMenager.DrawFrame(cEnemies.GetWindowName().c_str(), GetRGB(0,255,0), x, y-8, 16, 16);
}
